/**
 * CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

package com.cmcc.zysoft.infocollection.mobile.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.cmcc.zysoft.infocollection.aop.LoginLogAspect;
import com.cmcc.zysoft.infocollection.mobile.dao.MSystemUserDao;
import com.cmcc.zysoft.infocollection.model.Employee;
import com.cmcc.zysoft.infocollection.model.SystemUser;
import com.cmcc.zysoft.infocollection.util.MD5Tools;
import com.starit.common.dao.hibernate.HibernateBaseDao;
import com.starit.common.dao.service.BaseServiceImpl;

/**
 * @author 袁凤建
 * <br />邮箱: yuan.fengjian@ustcinfo.com
 * <br />描述: MSystemUserService.java
 * <br />版本: 1.0.0
 * <br />日期: 2013-8-12 下午1:52:48
 * <br />CopyRight © 2012 USTC SINOVATE SOFTWARE CO.LTD All Rights Reserved.
 */

@Service
public class MSystemUserService extends BaseServiceImpl<SystemUser, String> {

	@Resource
	private MSystemUserDao mSystemUserDao;
	
	/**
	 * 属性名称: deviceService.
	 * 类       型: DeviceService.
	 */
	@Resource
	private MDeviceService mDeviceService;
	
	/**
	 * 属性名称: mInterfaceLogService.
	 * 类       型: MInterfaceLogService.
	 */
	@Resource
	private MInterfaceLogService mInterfaceLogService;
	
	/**
	 * 属性名称: mEmployeeService.
	 * 类       型: MEmployeeService.
	 */
	@Resource
	private MEmployeeService mEmployeeService;
	
	@Override
	public HibernateBaseDao<SystemUser, String> getHibernateBaseDao() {
		return this.mSystemUserDao;
	}
	
	/**
	 * 三码校验接口-如果本地存在手机号,则在loading页对用户手机号、IMSI号、IMEI号做鉴权.
	 * @param usercode 手机号
	 * @param IMEI 手机设备号
	 * @param IMSI SIM卡号
	 * @return map
	 */
	public Map<String, Object> check(String usercode, String IMEI, String IMSI) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			SystemUser systemUser = list.get(0);
			if(this.mDeviceService.check(systemUser.getUserId(), IMEI, IMSI)) {
				map.put("status", "1");
				map.put("message", "鉴权通过");
			} else {
				map.put("status", "2");
				map.put("message", "IMEI已变更,请重新注册");
			}
		} else {
			map.put("status", "2");
			map.put("message", "用户不存在或已删除");
		}
		return map;
	}
	
	/**
	 * 登录接口-通过该接口,用户需要通过此接口将手机号和IMSI号、IMEI号绑定.
	 * @param usercode 手机号
	 * @param password 动态验证码
	 * @param IMEI 手机设备号
	 * @param IMSI SIM卡号
	 * @param version 版本号
	 * @return map
	 */
	@Transactional
	public Map<String, Object> mlogin(String usercode, String password, String IMEI, String IMSI, String version) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<SystemUser> list = this.findByNamedParam(new String[]{"userName", "delFlag"}, new Object[]{usercode, "0"});
		if(!list.isEmpty()) {
			SystemUser systemUser = list.get(0);
			if(this.mDeviceService.check(systemUser.getUserId(), IMEI, IMSI)) {
				String passSalt = systemUser.getPassSalt().toString();
				String realPass = systemUser.getPassword().toString();
				String pass = MD5Tools.encodePassword(password, passSalt);
				if(pass.equals(realPass)) {
					//更新版本号
					Employee employee = this.mEmployeeService.getEntity(systemUser.getEmployeeId());
					employee.setIdCard(version);
					this.mEmployeeService.updateEntity(employee);
					//添加接口调用日志
					this.mInterfaceLogService.addLog(systemUser.getUserId(), systemUser.getCompanyId(), "登录");
					map.put("success", true);
					map.put("status", "1");
					map.put("result", this.mSystemUserDao.getEmpInfo(usercode));
					map.put("message", "登录成功");
				} else {
					map.put("type", LoginLogAspect.BAD_CAPATCH);
					map.put("status", "2");
					map.put("success", false);
					map.put("message", "密码错误");
				}
			} else {
				map.put("type", LoginLogAspect.USER_NOT_REGISTER);
				map.put("status", "2");
				map.put("success", false);
				map.put("message", "您还未注册,请注册后登录");
			}
		} else {
			map.put("type", LoginLogAspect.USERNAME_NOT_FOUND);
			map.put("status", "2");
			map.put("success", false);
			map.put("message", "用户不存在或已删除");
		}
		map.put("userName", usercode);
		return map;
	}
}